
import axios from 'axios';
import { Message } from '@arco-design/web-vue';
import router from '@/router';


axios.defaults.withCredentials = true;
axios.defaults.baseURL = import.meta.env.VITE_BASE_URL;
export const service = axios.create({
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
    },
});

service.interceptors.request.use(
    (config) => {
        const userStore = useUserStore();
        logger('请求配置' + config.url, userStore.token, config);
        if (userStore.token) {
            config.headers['Authorization'] = `Bearer ${userStore.token}`;
        }
        return config;
    },
    (error) => {
        logger('请求出错', error);
        return Promise.reject(error);
    },
);


service.interceptors.response.use(
    (response) => {
        logger('服务响应', response, response.data);
        if (response.status === 200 && response.data.ok) {
            return response.data;
        }
        // 处理错误信息
        switch (response.data.code) {
            case 401:
                // Message.error('请先登录');
                router.push('/error/401');
                break;
            case 403:
                router.push('/error/403');
                // Message.error('没有权限');
                break;
            case 500:
                Message.error(response.data.data ? response.data.data : response.data.message);
                return response.data;
            default:
                Message.error(response.data.message);
                break;
        }

    },
    (error) => {
        // 服务器或网络错误
        if (error.response) {
            logger('服务响应异常', error);
            switch (error.response.status) {
                case 401:
                    Message.error('请先登录');
                    router.push('/console/login');
                    break;
                case 403:
                    Message.error('没有资源访问权限');
                    break;
                case 500:
                    Message.error('服务器异常，请联系管理员');
                    // router.push('/error/500');
                    break;
                default:
                    Message.error('服务器错误，请联系管理员');
                    break;
            }
        } else {
            logger('服务无响应', error);
            if (error.message.includes('Network Error')) {
                Message.error('服务无响应，网络错误');
            } else if (error.message.includes('timeout')) {
                Message.error('服务无响应，请求超时');
            } else {
                Message.error('服务无响应');
            }
        }

        return Promise.reject(error);
    },
);

const request = {
    get: (url: string, params?: any) => new Promise((resolve, reject) => {
        service.get(url, { params }).then((data) => {
            resolve(data);
        }).catch((error) => {
            reject(error);
        })
    }),
    postJson: (url: string, data?: any, config?: any) => new Promise((resolve, reject) => {
        service.post(url, data, { headers: { 'Content-Type': 'application/json' }, ...config }).then((data) => {
            resolve(data);
        }).catch((error) => {
            reject(error);
        });
    }),
    post: (url: string, data?: any, config?: any) => new Promise((resolve, reject) => {
        service.post(url, data, config).then((data) => {
            resolve(data);
        }).catch((error) => {
            reject(error);
        });
    }),
    putJson: (url: string, data?: any, config?: any) => new Promise((resolve, reject) => {
        service.put(url, data, { headers: { 'Content-Type': 'application/json' }, ...config }).then((data) => {
            resolve(data);
        }).catch((error) => {
            reject(error);
        });
    }),
    put: (url: string, data?: any, config?: any) => new Promise((resolve, reject) => {
        service.put(url, data, config).then((data) => {
            resolve(data);
        }).catch((error) => {
            reject(error);
        });
    }),
    patchJson: (url: string, data?: any, config?: any) => new Promise((resolve, reject) => {
        service.patch(url, data, { headers: { 'Content-Type': 'application/json' }, ...config }).then((data) => {
            resolve(data);
        }).catch((error) => {
            reject(error);
        });
    }),
    patch: (url: string, data?: any, config?: any) => new Promise((resolve, reject) => {
        service.patch(url, data, config).then((data) => {
            resolve(data);
        }).catch((error) => {
            reject(error);
        });
    }),
    delete: (url: string, params?: any) => new Promise((resolve, reject) => {
        service.delete(url, { params }).then((data) => {
            resolve(data);
        }).catch((error) => {
            reject(error);
        });
    }),
};
export default request;